﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/26 星期二 20:52:09
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 内部数据元数据访问对象
    /// </summary>
    public class MetadataDAO : DAOBase<S7_Metadata>, IMetadataDAO
    {
        #region Public functions
        /// <summary>
        /// 添加内部数据元
        /// </summary>
        /// <param name="entity">内部数据元</param>
        /// <returns>操作结果</returns>
        public override object Create(S7_Metadata entity)
        {
            string sql = @"
                INSERT INTO S7_Metadata
                        (MetadataId
                        ,ElementId
                        ,ColumnName
                        ,IsNull
                        ,KeyType)
                    VALUES
                        (@MetadataId
                        ,@ElementId
                        ,@ColumnName
                        ,@IsNull
                        ,@KeyType)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(entity.MetadataId, "MetadataId"),
                    DBUtil.CreateSqlParameter(entity.ElementId, "ElementId"),
                    DBUtil.CreateSqlParameter(entity.ColumnName, "ColumnName"),
                    DBUtil.CreateSqlParameter(entity.IsNull, "IsNull"),
                    DBUtil.CreateSqlParameter(entity.KeyType, "KeyType"));
            }
        }

        public override List<S7_Metadata> ReadAll()
        {
            string sql = @"
                SELECT *
                    FROM S7_Metadata md
                    LEFT JOIN S7_DataElement de
                        ON md.ElementId = de.ElementId
                    LEFT JOIN S7_ValueType vt
                        ON de.TypeId = vt.TypeId
                    LEFT JOIN S7_ValueDomain vd
                        on de.DomainId = vd.DomainId         
                ORDER BY md.MetadataId ASC";

            List<S7_Metadata> metadatas = new List<S7_Metadata>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql);

                while (reader.Read())
                {
                    var valueType = new S7_ValueType();
                    DBUtil.ReadData(reader, valueType);

                    var valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, valueDomain);

                    var metadata = new S7_Metadata();
                    DBUtil.ReadData(reader, metadata);

                    metadata.ValueType = valueType;
                    metadata.ValueDomain = valueDomain;

                    metadatas.Add(metadata);
                }
            }

            return metadatas;
        }

        /// <summary>
        /// 根据数据分组获取所有内部数据元
        /// </summary>
        /// <param name="groupId">数据分组编号</param>
        /// <returns>内部数据元列表</returns>
        public List<S7_Metadata> ReadAll(int groupId)
        {
            string sql = @"
                SELECT *
                    FROM S7_Metadata md
                    LEFT JOIN S7_DataElement de
                        ON md.ElementId = de.ElementId
                    LEFT JOIN S7_ValueType vt
                        ON de.TypeId = vt.TypeId
                    LEFT JOIN S7_ValueDomain vd
                        on de.DomainId = vd.DomainId
                WHERE de.GroupId = @GroupId
                ORDER BY md.MetadataId ASC";

            List<S7_Metadata> metadatas = new List<S7_Metadata>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupId, "GroupId"));

                while (reader.Read())
                {
                    var valueType = new S7_ValueType();
                    DBUtil.ReadData(reader, valueType);

                    var valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, valueDomain);

                    var metadata = new S7_Metadata();
                    DBUtil.ReadData(reader, metadata);

                    metadata.ValueType = valueType;
                    metadata.ValueDomain = valueDomain;

                    metadatas.Add(metadata);
                }
            }

            return metadatas;
        }
        #endregion
    }
}
